<?php

namespace Blog\Model;

use Hx\Mvc\Model\Table;

class Comment extends Table {

    public function __construct() {
        $this->setTable('fxh_comment');
    }

    public function saveOne($data) {
        $data['createTime'] = time();
        $data['status'] = $this->getStatus($data['email']);
        if (!$data['parentId']) {
            $data['parentId'] = 0;
        };
        $data['content'] = $this->filterContent($data['content']);
        if ($data['parentId'] > 0) {
            $data['content'] = $this->appendParentContent($data['parentId']) . $data['content'];
        }
        $this->insert($data);
        $data['id'] = $this->db->lastId();
        if ($data['status'] == 'approved' && $data['parentId'] == 0) {
            $this->updateCount();
        }
        $data['avatar'] = $this->avatar($data['email']);
        return $data;
    }

    private function getStatus($email) {
        $num = $this->count(array(
            'where' => 'email = :email AND status = "approved" AND type = "comment"',
            'param' => array('email' => $email)
        ));
        return $num > 0 ? 'approved' : 'check';
    }

    public function getTree($blogId) {
        $commentData = $this->select(array(
            'where' => 'relateId = :id AND type = "comment" AND status ="approved"',
            'param' => array('id' => $blogId)
        ), 'id,author,email,url,content,createTime,parentId');
        if ($commentData) {
            $tempData = array();
            foreach ($commentData as $c) {
                #获取头像字符串
                $c->avatar = $this->avatar($c->email);
                unset($c->email);
                $tempData[$c->id] = $c;
            }
            $commentData = array();
            foreach ($tempData as $k => $c) {
                if ($tempData[$c->parentId]) {
                    #将子评论移动到父评论麾下
                    $tempData[$c->parentId]->children[] =& $tempData[$k];
                    continue;
                }
                if ($c->parent > 0) {
                    continue;
                }
                $commentData[] =& $tempData[$k];
            }
        }
        return $commentData;
    }

    private function avatar($email) {
        return "http://1.gravatar.com/avatar/" . md5(strtolower(trim($email))) . "?s=40";
    }

    private function updateCount() {
        $sql1 = $this->useSql('commentNum', 'fxh_blog')
            ->where('status = "publish"')->update();
        $sql2 = $this->useSql('*', 'fxh_comment AS c')
            ->where('c.relateId = fxh_blog.id AND c.type = "comment" AND c.parentId = 0 AND c.status = "approved"')
            ->count();
        $sql = str_replace(':commentNum', '(' . $sql2 . ')', $sql1);
        $this->db->exec($sql);
    }

    private function filterContent($content) {
        $content = strip_tags($content, '<br><img><a><p>');
        $content = htmlspecialchars($content);
        $content = nl2br($content, TRUE);
        return $content;
    }

    private function appendParentContent($parentId) {
        $comment = $this->only(array(
            'where' => 'id = :id',
            'param' => array('id' => $parentId)
        ), 'id,author');
        $content = '<a class="comment-parent-author" href="#comment-' . $comment->id . '">@' . $comment->author .
            '</a>';
        return $content;
    }
}

?>
